package com.streamhub.client;

import java.util.List;

import com.google.gwt.core.client.JavaScriptObject;

class JavaScriptUtils {
	public static native JavaScriptObject createObject() /*-{
		return new Object();
	}-*/;

	public static native JavaScriptObject createArray(int length) /*-{
		return new Array(length);
	}-*/;
	
	public static JavaScriptObject toArray(List<String> list) {
		int index = 0;
		JavaScriptObject array = createArray(list.size());

		for (String string : list) {
			arraySet(array, index++, string);
		}
		
		return array;
	}
	
	public static JavaScriptObject toArray(String[] topics) {
		int index = 0;
		JavaScriptObject array = createArray(topics.length);

		for (String string : topics) {
			arraySet(array, index++, string);
		}
		
		return array;
	}

	public static native JavaScriptObject arraySet(JavaScriptObject array, int index, String value) /*-{
		array[index] = value;
	}-*/;
	
	public static native JavaScriptObject set(JavaScriptObject object, String param, String value) /*-{
		object[param] = value;
	}-*/;
	
	public static native JavaScriptObject set(JavaScriptObject object, String param, double value) /*-{
		object[param] = value;
	}-*/;
	
	public static native JavaScriptObject set(JavaScriptObject object, String param, int value) /*-{
		object[param] = value;
	}-*/;

	public static native JavaScriptObject set(JavaScriptObject object, String param, boolean value) /*-{
		object[param] = value;
	}-*/;
	
	public static native JavaScriptObject set(JavaScriptObject object, String param, JavaScriptObject array) /*-{
		object[param] = array;
	}-*/;
	
	public static native String getString(JavaScriptObject object, String param) /*-{
		return object[param];
	}-*/;
	
	public static native double getDouble(JavaScriptObject object, String param) /*-{
		return object[param];
	}-*/;
	
	public static native int getInt(JavaScriptObject object, String param) /*-{
		return object[param];
	}-*/;
	
	public static native boolean getBoolean(JavaScriptObject object, String param) /*-{
		return object[param];
	}-*/;
	
	public static native JavaScriptObject getArray(JavaScriptObject object, String param) /*-{
		return object[param];
	}-*/;
	
	public static native int getArrayLength(JavaScriptObject array) /*-{
		return array.length;
	}-*/;
	
	public static native String getArrayValue(JavaScriptObject array, int index) /*-{
		return array[index];
	}-*/;
	
	public static String[] toArray(JavaScriptObject array) {
		int length = getArrayLength(array);
		String[] javaArray = new String[length];
		
		for (int i = 0; i < javaArray.length; i++) {
			javaArray[i] = getArrayValue(array, i);
		}
		
		return javaArray;
	}
}
